package org.sbbs.app.demo.webapp.action;

import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.sbbs.app.demo.model.DemoTreeNode;
import org.sbbs.app.demo.service.DemoTreeNodeManager;
import org.sbbs.base.webapp.action.BaseJqgridEditAction;

public class DemoTreeNodeEditAction extends BaseJqgridEditAction {

	/**
	 *
	 */
	private static final long serialVersionUID = -4107680144329528262L;
	private DemoTreeNode model;
	private DemoTreeNodeManager demoTreeNodeManager;

	public final DemoTreeNode getModel() {
		return model;
	}

	public final void setModel(final DemoTreeNode model) {
		this.model = model;
	}

	public final DemoTreeNodeManager getDemoTreeNodeManager() {
		return demoTreeNodeManager;
	}

	public final void setDemoTreeNodeManager(
			final DemoTreeNodeManager demoTreeNodeManager) {
		this.demoTreeNodeManager = demoTreeNodeManager;
	}

	public final String edit() {
		if (getModelId() != null) {
			this.setModel(this.demoTreeNodeManager.get(Long.parseLong(this
					.getModelId())));
			this.setEditType(EDIT_TYPE_EDIT);
		} else {
			this.setModel(new DemoTreeNode());
			this.setEditType(EDIT_TYPE_NEW);
		}
		return SUCCESS;

	}

	public final String save() {
		try {
			HttpServletRequest req = this.getRequest();
			boolean isNew = (this.getEditType() == 1);
			if (isNew) {
				this.getDemoTreeNodeManager().insertNode((DemoTreeNode) model);
			}
			this.setMessage(getText((isNew)
					? "demoTreeNode.added"
					: "demoTreeNode.updated",
					"no msg key found,save successed."));
		} catch (Exception e) {
			this
					.setMessage(getText("error.saved", new String[]{e
							.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return SUCCESS;
	}

	public final String delete() {
		try {

			StringTokenizer ids = new StringTokenizer(this.getDelIds(), ",");
			while (ids.hasMoreTokens()) {
				Long removeId = Long.parseLong(ids.nextToken());
				this.getDemoTreeNodeManager().delete(removeId);
			}
			this.setMessage(getText("demoTreeNode.deleted",
					"no msg key found,delete successed."));
		} catch (Exception e) {
			this.setMessage(getText("error.deleted", new String[]{e
					.getMessage()}));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return SUCCESS;
	}

	public final String rebuildTree() {
		this.getDemoTreeNodeManager().reBuildAllTree();
		this.setMessage("rebuild nest set tree success.");
		return SUCCESS;
	}

	private List fullTreeList;

	public final List getFullTreeList() {
		return fullTreeList;
	}

	/*
	 * public void setFullTreeList(List fullTreeList) { this.fullTreeList =
	 * fullTreeList; }
	 */

	public final String displayFullTree() {
		this.fullTreeList = this.getDemoTreeNodeManager().getAll();
		return SUCCESS;
	}

	private Long from, target;

	public final Long getFrom() {
		return from;
	}

	public final void setFrom(final Long from) {
		this.from = from;
	}

	public final Long getTarget() {
		return target;
	}

	public final void setTarget(final Long target) {
		this.target = target;
	}

	public final String move() {
		this.getDemoTreeNodeManager().move(from, target);
		return SUCCESS;
	}
}
